package com.hlg.xboot.core.media;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Bashan
 */
public class VideoInfo extends FFMPEGInfo {
    private int hours;
    private int minutes;
    private float seconds;

    private int width;
    private int height;

    public VideoInfo(String videoUrl) {
        super(videoUrl);
    }

    public VideoInfo(String app, String videoUrl) {
        super(app, videoUrl);
    }

    public void loadVideoInfo() throws IOException, InterruptedException {
        String tmpFile = this.videoUrl + ".tmp";
        if (this.videoUrl.startsWith("http")) {
            tmpFile = "/tmp/" + this.videoUrl.substring(this.videoUrl.lastIndexOf("/") + 1) + ".tmp";
        }
        ProcessBuilder processBuilder = new ProcessBuilder(this.app, "-y", "-i", this.videoUrl, "-vframes", "1",
                "-ss", "0:0:0", "-an", "-vcodec", "png", "-f", "rawvideo", "-s", "100*100", tmpFile);
        Process process = processBuilder.start();
        InputStream stderr = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line;
        StringBuffer sb = new StringBuffer();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        new File(tmpFile).delete();

        // 提取时长
        Pattern pattern = Pattern.compile("Duration: (.*?),");
        Matcher matcher = pattern.matcher(sb);
        if (matcher.find()) {
            String time = matcher.group(1);
            String[] parts = time.split(":");
            this.hours = Integer.parseInt(parts[0]);
            this.minutes = Integer.parseInt(parts[1]);
            this.seconds = Float.parseFloat(parts[2]);
        }

        // 撮分辨率
        pattern = Pattern.compile(", (\\d+x\\d+).*\\[SAR");
        matcher = pattern.matcher(sb.toString());
        if (matcher.find()) {
            String wh = matcher.group(1);
            String[] parts = wh.split("x");
            this.width = Integer.parseInt(parts[0]);
            this.height = Integer.parseInt(parts[1]);
        }
        process.waitFor();
    }

    public int getHours() {
        return hours;
    }

    public int getMinutes() {
        return minutes;
    }

    public float getSeconds() {
        return seconds;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public String getDuration() {
        String hour = this.hours > 9 ? String.valueOf(this.hours) : "0" + this.hours;
        String minutes = this.minutes > 9 ? String.valueOf(this.minutes) : "0" + this.minutes;
        String seconds = this.seconds > 9 ? String.valueOf((int) Math.ceil(this.seconds)) : "0" + (int) Math.ceil(this.seconds);
        return  hour + ":" + minutes + ":" + seconds;
    }

    @Override
    public String toString() {
        return "duration: " + this.getDuration() + "   pix:" + this.width + "x" + this.height;
    }
}